<?php

function addVipTemplateSelect($allVips, $selectedVip, $addHidden, $disabled, $isNew){
    echo '<select name="vip_tmpl'.($isNew?'_new':'').'" id="vip_tmpl'.($isNew?'_new':'').'" '.($disabled?' disabled="true"':'').' >';
    foreach($allVips as $vip_t){
        echo '<option value="'.$vip_t['id'].'" '.($selectedVip == $vip_t['id']?' selected="selected"':'').' >'.$vip_t['name'].'</option>';
    } 
    echo '</select>';
    if($addHidden){
        echo '<input type="hidden" name="hvip_tmpl'.($isNew?'_new':'').'" id="hvip_tmpl'.($isNew?'_new':'').'" value="'.$selectedVip.'" />';
    } 
}

function addServerSelect($allServers, $selectedSrv, $addHidden, $disabled){
    echo '<select name="vip_srv_new" id="vip_srv_new" '.($disabled?' disabled="true"':'').' >';
    foreach($allServers as $srv){
        echo '<option value="'.$srv['id'].'" '.($selectedSrv == $srv['id']?' selected="selected"':'').' >'.$srv['name'].'</option>';
    } 
    echo '</select>';
    if($addHidden){
        echo '<input type="hidden" name="hvip_srv_new" id="hvip_srv_new" value="'.$selectedSrv.'" />';
    } 
}
function createNewVip(){
    if(!hasAccess('add_vips'))
            return;
    global $prefix;
    global $lang;
   
    $name='';
    if(isset($_POST['name']))
        $name = $_POST['name'];
    if($name == ''){
        echo $lang['NAME_REQ'];
        echo '<br/>';
        return;
    }
    $pass='';
    $pass_repeate='';
    if(isset($_POST['pass']))
        $pass = trim($_POST['pass']);
    if(isset($_POST['pass_repeate']))
        $pass_repeate = trim($_POST['pass_repeate']);   
    if($pass==''){
        echo $lang['PASS_REQ'];
        echo '<br/>';
        return;
    }  
    if($pass!=$pass_repeate){
        echo $lang['PASS_MISMATCH'];
        echo '<br/>';
        return;
    }   
    $fields = array(
        'name' => '',
        'pass' => ''
    );
    $query = 'SELECT 1 FROM '.$prefix.'_VIPS WHERE name = \''.mysql_escape_string($name).'\'';
    $result = mysql_query($query);
    if($result){
        if(mysql_num_rows($result)>0){
            echo $lang['NAME_TAKEN'];
            echo '<br/>';
            return;
        }
    }
    mysql_query("SET AUTOCOMMIT=0");
    mysql_query("START TRANSACTION");
    
    $commit=true;
    $query = createInsertStatement($fields, $prefix.'_VIPS', true);
    $result = mysql_query($query);
    if(!$result){
        echo mysql_error();
		echo '<br/>';
        $commit=fasle;
    }
    if($commit){
        $vip_id=mysql_insert_id();
        if(isset($_POST['vip_srv_new']) ||
            isset($_POST['vip_tmpl_new'])||
            isset($_POST['days'])){
            
            if(isset($_POST['vip_srv_new']) && is_numeric($_POST['vip_srv_new']) &&
                isset($_POST['vip_tmpl_new'])&& is_numeric($_POST['vip_tmpl_new']) &&
                isset($_POST['days']) && is_numeric($_POST['days'])){
                $query = 'INSERT INTO '.$prefix.'_VIPS_SRV (vip, start_date, end_date, server, vip_tmpl) VALUES (
                '.$vip_id.', SYSDATE(), DATE_ADD(SYSDATE(),INTERVAL '.$_POST['days'].' DAY), '.$_POST['vip_srv_new'].', '.$_POST['vip_tmpl_new'].')';
                
                $result = mysql_query($query);
                if(!$result){
                    echo mysql_error();
            		echo '<br/>';
                    $commit=fasle;
                }
            } else {
                if(!isset($_POST['vip_srv_new'])){
                    echo $lang['SERVER_REQ'].'<br />';
                }
                if(!isset($_POST['vip_tmpl_new'])){
                    echo $lang['VIP_TMPL_REQ'].'<br />';
                }
                if(!isset($_POST['days'])){
                    echo $lang['TIME_REQ'].'<br />';
                }
            }
        }
    }
    
    if($commit){
        mysql_query("COMMIT");
    } else {        
        mysql_query("ROLLBACK");
    } 
}
function deleteVipSrv(){
    if(!hasAccess('edit_vips'))
        return;
     global $lang;
     global $prefix;
     $query = 'DELETE FROM '.$prefix.'_VIPS_SRV where id = '.$_POST['vip_srv_id'];
     $result = mysql_query($query);
    	if(!$result){
            echo mysql_error();
            echo '<br/>';		
        }
}

function updateVipSrv(){
    global $lang;
    global $prefix;
    if(hasAccess('edit_vips') && isset($_POST['vip_srv_id'])){
        if(!isset($_POST['vip_tmpl'])){
            echo $lang['VIP_TMPL_REQ'];
            return;
        }
        $query = 'UPDATE '.$prefix.'_VIPS_SRV SET vip_tmpl='.$_POST['vip_tmpl'];
        if(isset($_POST['extend_time']) && $_POST['extend_time'] != ''&& is_numeric($_POST['extend_time'])){
            $query = $query. ', end_date = DATE_ADD(GREATEST(end_date, sysdate()), INTERVAL '.$_POST['extend_time'].' DAY)';
        }
        $query = $query. ' WHERE id = '.$_POST['vip_srv_id'];
        $result = mysql_query($query);
    	if(!$result){
            echo mysql_error();
            echo '<br/>';		
        }
    }
}

function updateVip(){
    if(!hasAccess('edit_vips'))
            return;
    global $lang;
    global $prefix;
    if(hasAccess('edit_vips') && isset($_POST['vip_id'])){
        if(isset($_POST['vip_srv_new']) && is_numeric($_POST['vip_srv_new']) &&
            isset($_POST['vip_tmpl_new'])&& is_numeric($_POST['vip_tmpl_new']) &&
            isset($_POST['days']) && is_numeric($_POST['days'])){
            
            $query = 'SELECT 1 from '.$prefix.'_VIPS_SRV WHERE vip='.$_POST['vip_id'].' AND server='.$_POST['vip_srv_new'];
            $result = mysql_query($query);
            if(!$result){
                echo mysql_error();
        		echo '<br/>';
               return;
            } else {
                if(mysql_num_rows($result)>0){
                    echo $lang['DUPLICATE_VIP_SRV'];
                    return;
                }
            }
            $query = 'INSERT INTO '.$prefix.'_VIPS_SRV (vip, start_date, end_date, server, vip_tmpl) VALUES (
            '.$_POST['vip_id'].', SYSDATE(), DATE_ADD(SYSDATE(),INTERVAL '.$_POST['days'].' DAY), '.$_POST['vip_srv_new'].', '.$_POST['vip_tmpl_new'].')';
            
            $result = mysql_query($query);
            if(!$result){
                echo mysql_error();
        		echo '<br/>';
                return;
            }
        } else {
            if(!isset($_POST['vip_srv_new']) || !is_numeric($_POST['vip_srv_new'])){
                echo $lang['SERVER_REQ'].'<br />';
            }
            if(!isset($_POST['vip_tmpl_new']) || !is_numeric($_POST['vip_tmpl_new'])){
                echo $lang['VIP_TMPL_REQ'].'<br />';
            }
            if(!isset($_POST['days']) || !is_numeric($_POST['days'])){
                echo $lang['TIME_REQ'].'<br />';
            }
        }
    }
}

function deleteVip(){
    if(!hasAccess('del_vips'))
            return;
    global $prefix;
    
    $query = 'DELETE FROM '.$prefix.'_VIPS WHERE id = '.$_POST['id'];
    $result = mysql_query($query);
	if(!$result){
        echo mysql_error();
        echo '<br/>';		
    }
}

function showVipDetails($id, $isEdit){
    global $lang;
    global $prefix;
    $isNew = false;
    if($id != null){
        $query = 'SELECT * FROM '.$prefix.'_VIPS WHERE id = '.$id;
    	$result = mysql_query($query);
    	if(!$result){
            echo mysql_error();
            echo '<br/>';
        } else {
            $vip = mysql_fetch_array($result);
        }
    }
    else {
        $vip = null;
        $isNew = true;
    }
    $query = 'SELECT id, name FROM '.$prefix.'_VIP_TMPL';
    $result = mysql_query($query);
    $vips_templates = array();
    if($result){
        while($vip_t = mysql_fetch_array($result)){
            array_push($vips_templates, $vip_t);
        }
    }
    $query = 'SELECT id, name FROM '.$prefix.'_SRV';
    $result = mysql_query($query);
    $servers = array();
    if($result){
        while($srv = mysql_fetch_array($result)){
            array_push($servers, $srv);
        }
    }    
   
    if($isNew){
    	echo ' <form class="fill" id="form_vip" name="form_vip" method="POST" action="main.php?action=vip">
        <table class="fill">';
        echo '
        <tr>';
        addText('NAME', 'name', null, '', null, false);
        echo '
        </tr>
        <tr>
            <th class="right">'.$lang['PASSWORD'].'</td>
            <td>
                <input type="password" id="pass" name="pass"/>
            </td>
        </tr>
        <tr>
            <th class="right">'.$lang['PASSWORD_REPEATE'].'</td>
            <td>
                <input type="password" id="pass_repeate" name="pass_repeate"/>
            </td>
        </tr>';
    } else {
    echo '
    <center><table class="fill"><tr><td> 
        <table class="fill">
        <tr>  
            <th class="right half">'.$lang['NAME'].'</th>
            <td class="left readonly_value">'.$vip['name'].'</td>
        </tr>
        <tr>
            <th class="tittle" colspan="2">'.$lang['ASSIGNED_VIPS'].'</th>
        </tr>';
     
        $query = 'SELECT v.*, s.name as server_name, t.name as vip_name FROM '.$prefix.'_VIPS_SRV v, '.$prefix.'_VIP_TMPL t, 
        '.$prefix.'_SRV s WHERE v.server = s.id 
        and t.id = v.vip_tmpl 
        and v.vip = '.$id;
        $result = mysql_query($query);
        if($result){
             while($vip_srv = mysql_fetch_array($result)){
                addVipSrv($vips_templates, $vip_srv);
             }
        } else {
            echo mysql_error();
            echo '<br/>';
        }
    }
    if($isNew || hasAccess('edit_vips')){
        if(!$isNew){
            echo ' </table>
            <form class="fill" style="min-height:10px" name="form_vip" id="form_vip" method="POST" action="main.php?action=vip">
            <table class="fill">';
        }
        echo '
                <tr>
                    <td colspan="2">&nbsp;</td>
                </tr>
                <tr>
                    <th class="tittle" colspan="2">'.$lang['ADD_VIP_SRV'].'</th>
                </tr>
                <tr>
                    <th class="right">'.$lang['SERVER'].'</th>
                    <td>';
        addServerSelect($servers, null, true, true);           
        echo'       </td>
                </tr>
                <tr>
                    <th class="right">'.$lang['VIP_TMPL_NAME'].'</th>
                    <td>';
        addVipTemplateSelect($vips_templates, null, true, true, true);                        
        echo '      </td>
                </tr>
                <tr>';
        addText('VIP_TIME', 'days', null, '', null, true);                
        echo '  </tr>';
    }
    if($isNew || hasAccess('edit_vips')){
        echo '<tr><td colspan="2" class="right">';
    }
    if($isNew) {
        echo '<input class="btn" type="submit" name="ins" value="'.$lang['B_SAVE'].'"/>';
    } else if(hasAccess('edit_vips')){
    
        echo '<input type="hidden" name="vip_id" value="'.$id.'"/>
        <input class="btn" type="submit" name="add_vip_srv" value="'.$lang['B_ADD_VIP_SRV'].'"/>';
    } 
    if($isNew || hasAccess('edit_vips')){
        echo '</td></tr>';
    }
    echo '</table></form>';
    if(!$isNew){
        echo '</td></tr></table></center>';
    }
    if(hasAccess('edit_vips')){
        echo '<script type="text/javascript">
                enableInputs(true, "form_vip");
        </script>';
    } 
}

function addVipSrv($vips_templates, $vip_srv){
    global $lang;

    echo '
    <tr onclick="SlideUpDown(\'vip_srv'.$vip_srv['id'].'\')">
        <th class="tittle" colspan="2">'.$vip_srv['server_name'].'</th>
    </tr>
    <tr name="vip_srv'.$vip_srv['id'].'" id="vip_srv'.$vip_srv['id'].'" style="display:none">
        <td colspan="2">
            <div class="fill" style="display:none">
                <form class="fill" style="min-height:10px" id="form_vip_srv'.$vip_srv['id'].'" name="form_vip_srv'.$vip_srv['id'].'" method="POST" action="main.php?action=vip">
                    <table class="tab_details fill">
                    <tr><td colspan="2"><input type="hidden" name="vip_srv_id" value="'.$vip_srv['id'].'" />
                    <input type="hidden" name="vip_id" value="'.$vip_srv['vip'].'" />
                    </td></tr>
                    
                        <tr>
                            <th class="right">'.$lang['START_DATE'].'</th>
                            <td class="left readonly_value">'.$vip_srv['start_date'].'</td>
                        </tr>
                        <tr>
                            <th class="right">'.$lang['END_DATE'].'</th>
                            <td class="left readonly_value">'.$vip_srv['end_date'].'</td>
                        </tr>';
    if (hasAccess('edit_vips')){
    
        echo '          <tr>';
        addText('EXTEND_TIME', 'extend_time', $vip_srv, '', null, true);
        echo '          </tr>
                        <tr>
                            <th class="right ">'.$lang['VIP_TMPL_NAME'].'</th>
                            <td class="left">';
        addVipTemplateSelect($vips_templates, $vip_srv['vip_tmpl'], true, true, false);                        
        echo '              </td>
                        </tr>
                        <tr>
                            <td colspan="2" class="right">
                                <input class="btn" type="button" name="edit" value="'.$lang['B_EDIT'].'" onclick="toogleInputs(\'form_vip_srv'.$vip_srv['id'].'\');"/> 
                                <input class="btn save" type="submit" disabled="true" name="upd_vip_srv" id="upd_vip_srv" value="'.$lang['B_SAVE'].'"/>
                                <input class="btn" type="submit" name="del_vip_srv" value="'.$lang['B_DELETE'].'" onclick="return confirmDelete()"/>
                            </td>
                        </tr>
        ';
    }  else {
        echo '          <tr>
                            <th class="right">'.$lang['VIP_TMPL_NAME'].'</th>
                            <td class="left readonly_value">'.$vip_srv['vip_name'].'</td>
                        </tr>';
    }            
    echo '          </table>             
                </form>   
             </div>              
        </td>
    </tr>';
   
} 
     
?>
